"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 开启gpc情况下执行tpcc
Create At   : 2021/07/03
@zou_jialiang0505328126
Description :
    1.开启GPC
    2.重启数据库
    3.运行tpcc
    4.运行事务的同时查询视图
Expect      :
    1.成功
    2.成功
    3.成功，数据库正常，无error
    4.视图中存在内容
History     :
    modified by @wan005 at 2021/09/27:优化tpcc结果获取，优化等待时间
"""

import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GPCSet(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case268.py 开始执行------')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.db_primary_user_node = Node(node='PrimaryDbUser')
        self.constant = Constant()
        self.com_pri = Common('PrimaryRoot')
        self.commonshpri = CommonSH('PrimaryDbUser')

        result = self.commonshpri.executDbSql('show enable_thread_pool;')
        self.log.info(f"enable_thread_pool is {result}")
        self.enable_thread_pool = result.strip().splitlines()[-2]

        result = self.commonshpri.executDbSql(
            'show enable_global_plancache;')
        self.log.info(f"enable_global_plancache is {result}")
        self.enable_global_plancache = result.strip().splitlines()[-2]

    def test_gpcset(self):
        self.log.info('-------------1.开启GPC-----------')
        result = self.commonshpri.executeGsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG, "enable_thread_pool = on")
        self.assertTrue(result)
        result = self.commonshpri.executeGsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            "enable_global_plancache = on")
        self.assertTrue(result)

        self.log.info('--------------2.重启数据库------------------')
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        try:
            self.log.info('---------------3.执行TPCC-------------')
            create_data = ComThread(self.com_pri.startTPCC,
                                    args=(self.db_primary_root_node,
                                          macro.TPCC_PATH,))
            create_data.setDaemon(True)
            create_data.start()

            self.log.info('---------------4.查询视图-------------')
            time.sleep(30)
            cmd = f"select * from dbe_perf.global_plancache_status;"
            result = self.commonshpri.executDbSql(cmd)
            self.log.info(result)
            self.assertNotIn('(0 rows)', result)
        finally:
            create_data.join(60*10)
            result = create_data.get_result()
            self.log.info(result)
            self.assertIn(self.constant.TPCC_SUCCESS_MSG, result)

    def tearDown(self):
        self.log.info('-----------------------------环境清理--------------------')
        self.commonshpri.executeGsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            f"enable_thread_pool = {self.enable_thread_pool}")
        self.commonshpri.executeGsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            f"enable_global_plancache = {self.enable_global_plancache}")
        self.commonshpri.startDbCluster()
        self.log.info('---Opengauss_Reliability_Dbsys_Case268.py 执行结束-------')
